{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0eba10e4",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "- 优化ASR(自动语音识别)机器人\n",
    "- 连接图灵机器人\n",
    "- 语音合成\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9c9cadaa",
   "metadata": {},
   "source": [
    "# 优化ASR(自动语音识别)机器人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4797e318",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-05-24T11:20:37.809755Z",
     "start_time": "2022-05-24T11:20:37.799327Z"
    }
   },
   "outputs": [],
   "source": [
    "# 准备百度key和secret\n",
    "API_KEY = 'A6kdrD4MtkN72GyKpZL8Uaez'\n",
    "SECRET_KEY = 'FDVIx2P6ET6MtjVHGumSQ1FjxbE9xaQa'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3bc4b56e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from bauidu_speech import fetch_token,asr\n",
    "vin_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "vin_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "80c6b4eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29d97fb8",
   "metadata": {},
   "outputs": [],
   "source": [
    "asr(vin_token,'1.wav')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "385eb4a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "asr_result = eval(asr(vin_token,'1.wav'))['result'][0]\n",
    "asr_result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b93bdf2d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "67bc68e9",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b592e52",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9a408709",
   "metadata": {},
   "outputs": [],
   "source": [
    "TL_key = '******'\n",
    "TL_url = \"http://openapi.turingapi.com/openapi/api/v2\"\n",
    "\n",
    "payload={\n",
    "    \"reqType\":0,\n",
    "    \"perception\": {\n",
    "        \"inputText\": {\n",
    "            \"text\": \"苹果英语怎么说\"\n",
    "        },\n",
    "    },\n",
    "    \"userInfo\": {\n",
    "        \"apiKey\": TL_key,\n",
    "        \"userId\": \"1234\"\n",
    "    }\n",
    "}\n",
    "\n",
    "res = requests.post(TL_url,data=json.dumps(payload))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "480b6627",
   "metadata": {},
   "outputs": [],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "58d17420",
   "metadata": {},
   "outputs": [],
   "source": [
    "res.json()['results'][0]['values']['text']"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
